<?php
	require_once 'lib/file.php';
	require_once 'lib/common.php';

	$valid = isset($_GET["f"]);
	
	$f = new File(base64_decode($_GET["f"]));
	
	if ($valid)
		$valid = $valid && ($f->exists()) && (is_file($f->getFullFilename()));
	

	if (!$valid)
	{
		showFatalError('404', "Requested file doesn't exist");
		Exit;
	}

	if (!isFileViewAllowed($f->getFullFilename()))
	{
		showFatalError('Forbidden', "You don't have permission to access '{$f->getFilename()}' on this server");
		exit;
	}

	initSite("Editing {$f->getFilename()}");

	echo makeMenu( array( 
		array('Back to file explorer', "index.php?v={$f->getPath()}"), 
		array('Save', "javascript:savefile(\"{$_GET["f"]}\"); setSavedStatus();" )));
?>
<script type="text/javascript">
	function setStatus(newstatus)
	{
		if ($("span").eq(1).html != newstatus)
			$("span").eq(1).html(newstatus);    
	}
	
	$(document).ready
	(
		function()
		{
			$("#msgdiv").hide();
			setStatus('Document not modified');
		}
	);
	
	function pad(i)
	{
		return i > 10 ? i : "0" + i;
	}
	
	function setSavedStatus()
	{
		var d = new Date();
		
		setStatus('Saved at : ' 
			+ pad(d.getDate()) + "/" 
			+ pad(d.getMonth()) + "/" 
			+ pad(d.getYear()) + " - " 
			+ pad(d.getHours()) + ":" 
			+ pad(d.getMinutes()) + ":" 
			+ pad(d.getSeconds()));
	}
</script>
</div><!-- .top-menu end -->
<center>
	<div id="msgdiv" class="msg"></div>
</center><br>

<form name='editor' method ='post' action='manager.php?action=savefile'>
<input type='hidden' name='f' value="<?php echo $_GET['f'] ?>">
<textarea id="text" name='contents' onkeydown="setStatus('Modified')" class="filebox">
<?php 
	echo $f->getContents(true);
?>
</textarea>
</form>